# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Stuart Cardall <developer@it-offshore.co.uk>
pkgname=bitcoin
pkgver=23.0
pkgrel=2
pkgdesc="Decentralized P2P electronic cash system"
url="https://www.bitcoin.org/"
# x86: segfault in check
# https://gitlab.alpinelinux.org/alpine/aports/-/issues/12327#note_137460
arch="all !armhf !x86"
license="MIT"
makedepends="autoconf automake libtool boost-dev miniupnpc-dev
	qt5-qtbase-dev qt5-qttools-dev protobuf-dev libqrencode-dev libevent-dev chrpath zeromq-dev db-dev"
install="$pkgname.post-install $pkgname.post-upgrade $pkgname.pre-install"
subpackages="$pkgname-dev $pkgname-qt $pkgname-cli $pkgname-tx $pkgname-tests $pkgname-bench
	$pkgname-doc $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/bitcoin/bitcoin/archive/v$pkgver.tar.gz
	ssize_t.patch
	use-c-locale.patch
	$pkgname.initd
	$pkgname.conf
	"

# temp allow textrels on rv64
case $CARCH in
	riscv64) options="$options textrels" ;;
esac

# secfixes:
#   0.21.1-r0:
#     - CVE-2021-3195

prepare() {
	default_prepare
	./autogen.sh
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--with-incompatible-bdb \
		--with-gui=qt5 \
		--with-utils \
		--disable-ccache \
		--disable-static \
		--enable-hardening \
		--disable-openssl-tests \
		--enable-wallet
	make
}

check() {
	make check
}

package() {
	make install DESTDIR="$pkgdir"
	install -m755 -D "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -m600 -D "$srcdir"/$pkgname.conf "$pkgdir"/etc/$pkgname.conf
	rm -f "$pkgdir"/usr/lib/*.la
}

qt() {
	pkgdesc="Bitcoin with a Qt frontend & QR Code support"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/$pkgname-qt "$subpkgdir"/usr/bin/
}

cli() {
	pkgdesc="Bitcoin CLI"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/$pkgname-cli "$subpkgdir"/usr/bin/
}

tx() {
	pkgdesc="Bitcoin TX (Transaction Tool)"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/$pkgname-tx "$subpkgdir"/usr/bin/
}

tests() {
	pkgdesc="Bitcoin & Bitcoin-QT Unit Tests)"
	mkdir -p "$subpkgdir"/usr/bin
	chrpath -d "$pkgdir"/usr/bin/test_$pkgname
	mv "$pkgdir"/usr/bin/test_$pkgname-qt "$subpkgdir"/usr/bin/
	mv "$pkgdir"/usr/bin/test_$pkgname "$subpkgdir"/usr/bin/
}

bench() {
	pkgdesc="Bitcoin Benchmarking Tools)"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/bench_$pkgname "$subpkgdir"/usr/bin/
}

dev() {
	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/lib "$subpkgdir"/usr/
	mv "$pkgdir"/usr/include "$subpkgdir"/usr/
}

sha512sums="
c76defcb54618ac46ab9cc92e6df40d9a3d0e387bf9c2f253c5b66052c2ad449c533a9bf823f54a1a1c2e63c3ef0aa04d3146d686f6859c1b6a0e5aa39d75904  bitcoin-23.0.tar.gz
98aa5ad81bdb4ae961b791bc978c39117cdf2d83c2181f92bebbb0db107d9b6e86eda265fb3f93ff8a5ca8a7754d7148818b98095d57201dff9363d60b97e7dd  ssize_t.patch
6f3098a8da217c465b3d8ed66fd4d15f0a64f1aa813a18d72d0fa97cb612682d33554d6b4e9fbfd5c8c06950531ac15514cbafeb91ac0887c30f1527c3a20b13  use-c-locale.patch
c88ca4f0c8a3179dbac274db1719983352caa5074b236e59d8fe31ab45ffa99bd90c1a566c4459261a9dcdcc990b826f3466c77aa7a32cf9fb15529a510ac7fd  bitcoin.initd
726e7525d35b83f5f7548e050632e3d3c762cf6f924d6e347806caa103af4b36838263f7342a4127cd5bfb035d79254095ec63deb1d75975753a664e48f7cafd  bitcoin.conf
"
